import Vue from "vue"
//axios的配置
import axios from "axios"
//导入进度条插件
import NProgress from 'nprogress'
//导入进度条样式
// import 'nprogress/nprogress.css'
axios.defaults.baseURL = "https://www.liulongbin.top:8888/api/private/v1/"

axios.interceptors.request.use(config => {
    var arr = JSON.parse(sessionStorage.getItem('loginmsg'))
    var token
    if (arr) {
        token = arr.token
        console.log(token)
    } else {
        token = null
    }
    //当进入request拦截器，表示发送了请求，我们就开启进度条
    NProgress.start()
    config.headers.Authorization = token
    return config

})
axios.interceptors.response.use(config => {
    //当进入response拦截器，表示请求已经结束，我们就结束进度条
    NProgress.done()
    return config
})

// post :

export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        }).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}
// delete :

export function deletefn(url, params) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {
            params: params
        }).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

// get :

export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err.data)
            })
    })
}

// //put:
export function put(url, params) {
    return new Promise((resolve, reject) => {
        axios.put(url, params)
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err.data)
            })
    })
}
// 111111111111111
// 111111111111111

Vue.prototype.$http = axios;